<?php

/**
 * Name: 审批日志模型层
 * User: Silent
 * Date: 2025-09-20
 * Time: 22:44:35
 */

namespace App\Models\Safety;

use App\Models\Auth\Admins;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class ApprovalLogs extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'approval_logs';

    protected $guarded = [];

    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'old'        => 'json',
        'new'        => 'json',
    ];

    protected static $logName = 'questions';

    protected static function boot()
    {
        parent::boot();
    }

    protected function serializeDate(DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * 格式化状态
     *
     * @return string
     */
    public function getFormatStatusAttribute(): string
    {
        switch ($this->status) {
            case 'pending':
                return '待处理';
            case 'processing':
                return '处理中';
            case 'rectification':
                return '暂时处理';
            case 'resolved':
                return '已处理';
            case 'reject':
                return '驳回';
            default:
                return '-';
        }
    }

    /**
     * 格式化类型.
     *
     * @return string
     */
    public function getFormatTypeAttribute(): string
    {
        switch ($this->type) {
            case 'risk_sources':
                return '风险源';
            case 'accidents':
                return '生产事故';
            case 'hazards':
                return '安全隐患';
            case 'questions':
                return '问题清单';
            default:
                return '-';
        }
    }

    /**
     * 格式化操作动作.
     *
     * @return string
     */
    public function getFormatOperateActionAttribute(): string
    {
        switch ($this->operate_action) {
            case '1':
                return '创建';
            case '2':
                return '审核';
            case '3':
                return '复核';
            case '4':
                return '审批';
            case '5':
                return '驳回';
            default:
                return '-';
        }
    }

    
    /* 
     * 管理员
     */
    public function admin()
    {
        return $this->belongsTo(Admins::class, 'created_admin_id');
    }
}
